package com.hiray.taglayoutmanager.uitility;

import android.app.Application;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.widget.AppCompatTextView;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.Toast;

import com.hiray.taglayoutmanager.R;

/**
 * Created by hiray on 2017/12/18.
 *
 * @author hiray
 */

public class Toasty {
    private static Toast toast;
    private static Application mApp;
    private static AppCompatTextView mTextView;

    public static void init(Application app) {
        mApp = app;
    }

    static void toast(String msg) {
        createIfNull();
        mTextView.setText(msg);
        toast.show();
    }

    private static void createIfNull() {
        if (toast == null)
            synchronized (Toast.class) {
                if (toast == null)
                    toast = new Toast(mApp);
                mTextView = new AppCompatTextView(mApp);
                mTextView.setBackgroundResource(R.drawable.rice);
                mTextView.setPadding(dp2px(15f), dp2px(8f), dp2px(15f), dp2px(8f));
                mTextView.setTextColor(Color.parseColor("#551a8b"));
                mTextView.setGravity(Gravity.CENTER_VERTICAL);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    mTextView.setElevation(10f);
                    mTextView.setTranslationZ(10f);
                }
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(mTextView);
            }
    }

    private static int dp2px(float dp){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp,mApp.getResources().getDisplayMetrics());
    }
}
